<?php

class company_address extends CActiveRecord {
/**
 * The followings are the available columns in table 'customer':
 * @var string $cname
 * @var string $address
 * @var string $tele
 * @var string $fax
 * @var string $pic
 * @var integer $id
 * @var string $email
 * @var integer $postal
 * @var integer $date_added
 * @var integer $country_code
 */
    public $transaction = null;

    public $contacts,$email;
    public $type = array('Main','Branch','Collection','Delivery');
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'company_address';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
        array('address','safe'),
        array('country','length','max'=>25),
        array('email','length','max'=>254),
        array('postal, type', 'numerical', 'integerOnly'=>true),
        );
    }
    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            'telephone'=>array(self::HAS_MANY,'company_contacts','address_id','condition'=>'type<10'),
            'fax'=>array(self::HAS_MANY,'company_contacts','address_id','condition'=>'type>=10'),
            'members'=>array(self::HAS_MANY,'company_members','address_id','with'=>'contacts'),
            'company'=>array(self::BELONGS_TO, 'company','company_id'),
        );
    }
    /**
     * @return array customized attribute labels (name=>label)
     */

    public function attributeLabels() {
        return array(
        'cname'=>'company_address',
        'address'=>'Address',
        'tele'=>'Telephone',
        'fax'=>'Fax No.',
        'pic'=>'Person in Charge',
        'email'=>'E-mail',
        'postal'=>'Postal',
        'country_code'=>'Country',
        );
    }
    public function getName($company_addressID) {
        $this->findByPk($company_addressID);
    }
    public function validateall() {
        $validate = true;
        foreach($this->contacts as $contacts) {
            if(!$contacts->validate()) {
                $validate = false;
            }
        }
        foreach($this->email as $email) {
            if(!$email->validate()) {
                $validate = false;
            }
        }
        if(!$this->validate()) {
           $validate = false;
        }
        return $validate;
    }
    public function saveaddress($valid = false) {
    // Vallidate all informations
        $this->save($valid);
        $status = true;
        foreach($this->contacts as $contacts) {
            if(!empty($contacts->ref)) {
            $contacts->address_id = $this->id;
            if(!$contacts->save($valid))
                $status = false;
            }
        }
        foreach($this->email as $item) {
            if(!empty($item->ref)) {
            $item->address_id = $this->id;
            if(!$item->save($valid))
                $status = false;
            }
        }
        return $status;
    }
}